<template>
    <div>
        <el-dialog
            :title="!form.id ? '新增' : '修改'"
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">
                <div class="left">
                    <el-form ref="form" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="120px">
                        <el-form-item label="姓名" prop="realname">
                            <el-input placeholder="" v-model="form.realname" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="性别" prop="sex">
                            <el-select v-model="form.sex" placeholder="请选择">
                                <el-option label="保密" :value="0"></el-option>
                                <el-option label="男" :value="1"></el-option>
                                <el-option label="女" :value="2"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="生日" prop="birthday">
                            <el-date-picker
                                v-model="form.birthday"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="登录账号" prop="username">
                            <el-input :disabled="form.id && form.username==='admin'" placeholder="" v-model="form.username" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="手机号码" prop="mobile">
                            <el-input placeholder="" v-model="form.mobile" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱地址" prop="email">
                            <el-input placeholder="" v-model="form.email" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="角色" prop="roleIds" v-if="form.username !== 'admin'">
                            <el-select
                                v-model="form.roleIds"
                                multiple
                                filterable
                                allow-create
                                default-first-option
                                placeholder="请选择角色"
                                style="width: 100%">
                                <el-option
                                    v-for="item in roles"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>


                        <el-form-item label="状态" prop="status" v-if="form.username !== 'admin'">
                            <el-radio-group v-model="form.status">
                                <el-radio-button :label="1">正常</el-radio-button>
                                <el-radio-button :label="0">锁定</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="right">
                    <SingleUpload v-model="form.avatar" listType="picture-card"></SingleUpload>
                    <i style="color: red" class="mt10">尺寸：150x200</i>
                </div>
            </div>


            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>
import SingleUpload from '@/views/components/upload/single-upload'
export default {
    components: {
        SingleUpload
    },
    data() {
        return {
            visible: false,
            submitLoading: false,

            form: {
                realname: null,
                username: null,
                password: null,
                salt: null,
                mobile: null,
                email: null,
                avatar: null,
                sex: 1,
                birthday: null,
                status: 1,
                roleIds: []
            },
            rules: {
                realname: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
                username: [
                    { required: true, message: '用户账号不能为空', trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '手机号码不能为空', trigger: 'blur' }
                ],
                // email: [
                //     { required: true, message: '邮箱不能为空', trigger: 'blur' }
                // ],
                // birthday: [
                //     { required: true, message: '生日不能为空', trigger: 'blur' }
                // ]
            },
            roles: []
        }
    },

    methods: {
        init(id) {
            const refs = this.$refs
            this.form.id = id || null
            this.form.avatar = null
            this.visible = true

            this.$nextTick(() => {
                refs['form'].resetFields()
                this.getRoles()
                if(this.form.id) {
                    this.getInfo(this.form.id)
                }
            })
        },

        /**
         * 详细信息
         */
        async getInfo(id) {
            if (id) {
                const res = await this.$http({
                    url: '/system/sysuser/info/' + id,
                    method: 'GET'
                })
                this.form = Object.assign({}, res.data)
            }
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            const refs = this.$refs
            this.submitLoading = true
            const valid = await refs['form'].validate().catch(() => {})
            if(valid) {
                const res = await this.$http({
                    url: `/system/sysuser/${!this.form.id ? 'save' : 'update'}`,
                    method: 'POST',
                    data: this.form
                })
                if (this.ResultMessage(res)) {
                    this.$emit('refreshDataList')
                    this.submitLoading = false
                    this.visible = false
                } else {
                    this.submitLoading = false
                }
            } else {
                this.submitLoading = false
            }
        },

        /**
         * 获取角色列表
         */
        async getRoles() {
            const res = await this.$http({
                url: '/system/sysrole/list',
                method: 'GET'
            })
            this.roles = res.data
        },

    }
};
</script>

<style lang="scss" scoped>
.table_form_wrap{
    display: flex;
    .left {
        flex: 1;
    }
    .right{
        width: 300px;
        margin-left: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;
        padding-top: 20px;
    }
}
</style>
